package com.example.reggie.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.BaseContext;
import com.example.reggie.common.R;
import com.example.reggie.dto.OrdersDto;
import com.example.reggie.entity.AddressBook;
import com.example.reggie.entity.OrderDetail;
import com.example.reggie.entity.Orders;
import com.example.reggie.entity.User;
import com.example.reggie.service.AddressBookService;
import com.example.reggie.service.OrderDetailService;
import com.example.reggie.service.OrdersService;
import com.example.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private UserService userService;
    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(int page, int pageSize,String name){

        Page page1 = new Page(page,pageSize);
        Page dtoPage = new Page();

        //根据用户Id操作不同表获取需要的数据
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());

        //操作User表 ，获取Username
        LambdaQueryWrapper<User> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(User::getId, BaseContext.getCurrentId());

        //操作AddressBook表，获取 phone address consignee
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper3.eq(AddressBook::getUserId, BaseContext.getCurrentId());


        lambdaQueryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        ordersService.page(page1,lambdaQueryWrapper);



        BeanUtils.copyProperties(page1,dtoPage,"records");

        List<Orders> list = page1.getRecords();
        OrdersDto ordersDto = new OrdersDto();
        List<OrdersDto> dtoList = list.stream().map((item->{
            BeanUtils.copyProperties(item,ordersDto);
            Long id = item.getId();
            LambdaQueryWrapper<OrderDetail> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(OrderDetail::getOrderId,id);
            List<OrderDetail> list1 = orderDetailService.list(lambdaQueryWrapper1);

            User user = userService.getOne(lambdaQueryWrapper2);

            AddressBook one = addressBookService.getOne(lambdaQueryWrapper3);


            ordersDto.setOrderDetails(list1);
            ordersDto.setUserName(user.getName());
            ordersDto.setPhone(one.getPhone());
            ordersDto.setConsignee(one.getConsignee());
            ordersDto.setAddress(item.getAddress());
            return ordersDto;
        })).collect(Collectors.toList());

        dtoPage.setRecords(dtoList);

        return R.success(dtoPage);
    }

    @GetMapping("/page")
    public R<Page<Orders>> Page(int page, int pageSize,String name){
        Page<Orders> page1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(page1);
        return R.success(page1);
    }

    @PutMapping
    public R<String> order(@RequestBody Orders orders){
        log.info("");
        Orders byId = ordersService.getById(orders.getId());
        byId.setStatus(orders.getStatus());
        ordersService.updateById(byId);
        return R.success("状态修改成功");
    }
}
